package com.easy.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Order;
import com.easy.common.service.IOrderService;
import com.easy.order.mapper.OrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Override
    public Order getOrderByOrderNo(String orderNo) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        return super.getOne(queryWrapper);
    }

    @Override
    public int addOrder(Order order) {
        // 插入新订单
        boolean result = super.save(order);
        return result ? 1 : 0;
    }

    @Override
    public int updateOrder(Order order) {
        // 由于orderNo是主键，使用update方法而不是updateById
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", order.getOrderNo());
        boolean result = super.update(order, queryWrapper);
        return result ? 1 : 0;
    }

    @Override
    public int deleteOrder(String orderNo) {
        // 根据orderNo删除订单
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        boolean result = super.remove(queryWrapper);
        return result ? 1 : 0;
    }

    @Override
    public List<Order> getAll(Order order) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        // 如果传入的order对象有orderNo，则按orderNo模糊查询
        if (order != null && order.getOrderNo() != null && !order.getOrderNo().isEmpty()) {
            queryWrapper.like("order_no", order.getOrderNo());
        }
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Order> getPage(Order order, IPage page) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        // 如果传入的order对象有orderNo，则按orderNo模糊查询
        if (order != null && order.getOrderNo() != null && !order.getOrderNo().isEmpty()) {
            queryWrapper.like("order_no", order.getOrderNo());
        }
        return super.page(page, queryWrapper);
    }
}
